//动态分页加载
function loaded (type) {
    var myScroll,
        upIcon = $("#up-icon"),
        downIcon = $("#down-icon"),
        distance = 30; //滑动距离
  //重设高度
    var detailBoxDivHeight = $('#detailBoxDiv').height();
    //var browser_width = $(document.body).height();
    if(detailBoxDivHeight <= 440){
    	$("#detailBoxDiv").css("minHeight","440px");
    }
    myScroll = new IScroll('#wrapper', { probeType: 3, mouseWheel: true, click:true });
    	
    myScroll.on("scroll",function(){
        var y = this.y,
            maxY = this.maxScrollY - y,
            downHasClass = downIcon.hasClass("reverse_icon"),
            upHasClass = upIcon.hasClass("reverse_icon");
        console.log(this.maxScrollY);
    	console.log(this.y);
    	console.log(maxY);
        if(y >= distance){
            !downHasClass && downIcon.addClass("reverse_icon");
            return "";
        }else if(y < distance && y > 0){
            downHasClass && downIcon.removeClass("reverse_icon");
            return "";
        }

        if(maxY >= distance){
            !upHasClass && upIcon.addClass("reverse_icon");
            return "";
        }else if(maxY < distance && maxY >=0){
            upHasClass && upIcon.removeClass("reverse_icon");
            return "";
        }
    });
    var pageIndex=1;
    var pageSize=20;
    function upAjax(){
    	//获取form的值
		var searchKey = $('#searchInput').val();
		var productQuery = {};
		productQuery["searchKey"] = searchKey;
		pageIndex++;
		productQuery["pageIndex"] = pageIndex;
		productQuery["pageSize"] = pageSize;
        var params = JSON.stringify(productQuery);
        $.ajax({
            type: "post",
            url: baselocation+'/product-query',
            data: params,
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            dataType: "json",
            contentType: "application/json; charset=utf-8"
        }).done(function(result) {
            if (result && result.success && result.data.productList.length > 0) {
            	var totalCount = result.data.totalCount;
				var onSaleCount = result.data.onSaleCount;
				var offSaleCount = result.data.offSaleCount;
				var caogaoCount = result.data.caogaoCount;
				$("#totalCountLi").html("商品总数："+totalCount);
				$("#onSaleCountLi").html("上架:"+onSaleCount);
				$("#offSaleCountLi").html("下架:"+offSaleCount);
				$("#caogaoCountLi").html("草稿:"+caogaoCount);
				
				var productList = result.data.productList;
				var jsonStr = "";
				if(productList.length > 0){
					for(var i=0;i<productList.length;i++){
						var product = productList[i];
						if(type == "product"){
							jsonStr += "<a href='"+baselocation+"/product/"+product.id+"/details' class='details-link'>";
						}else{
							jsonStr += "<a href='"+baselocation+"/order/0_"+product.id+"' class='details-link'>";
						}
						jsonStr += "<ul class='details-list-image'>";
						jsonStr += "<li>";
						jsonStr += "<img id='picMainImage' style='margin: 0 auto;' src='"+product.picMain+"' width='73.5' height='75'/>";
						jsonStr += "</li>";
						jsonStr += "<li>"+product.name+"</br>"+product.spec+"</li>";
						jsonStr += "<li><s>￥"+product.marketPrice+"</s></br>￥"+product.salePrice+"</li>";
						jsonStr += "<li>"+product.statusName+"</li>";
						jsonStr += "</ul>";
						jsonStr += "</a>";
					}
				}
				
                $('#detailBoxDiv').append(jsonStr);
                myScroll.refresh(totalCount);
            }else{
            	$("#pullUp-msg").html("别拉了,到底了,没有更多数据了！！！");
            }
        }).fail(function() {
        	$("#detailBoxDiv").html("<font style='font-size:0.16rem;'>数据请求失败，请重新刷新</font>");
        })
    }

    function downAjax(){
    	//获取form的值
		var searchKey = $('#searchInput').val();
		var productQuery = {};
		productQuery["searchKey"] = searchKey;
		pageIndex = 1;
		productQuery["pageIndex"] = pageIndex;
		productQuery["pageSize"] = pageSize;
        var params = JSON.stringify(productQuery);
        $.ajax({
            type: "post",
            url: baselocation+'/product-query',
            data: params,
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            dataType: "json",
            contentType: "application/json; charset=utf-8"
        }).done(function(result) {
        	if (result && result.success && result.data.productList.length > 0) {
        		var totalCount = result.data.totalCount;
				var onSaleCount = result.data.onSaleCount;
				var offSaleCount = result.data.offSaleCount;
				var caogaoCount = result.data.caogaoCount;
				$("#totalCountLi").html("商品总数："+totalCount);
				$("#onSaleCountLi").html("上架:"+onSaleCount);
				$("#offSaleCountLi").html("下架:"+offSaleCount);
				$("#caogaoCountLi").html("草稿:"+caogaoCount);
				
				var productList = result.data.productList;
				var jsonStr = "";
				if(productList.length > 0){
					for(var i=0;i<productList.length;i++){
						var product = productList[i];
						if(type == "product"){
							jsonStr += "<a href='"+baselocation+"/product/"+product.id+"/details' class='details-link'>";
						}else{
							jsonStr += "<a href='"+baselocation+"/order/0_"+product.id+"' class='details-link'>";
						}
						jsonStr += "<ul class='details-list-image'>";
						jsonStr += "<li>";
						jsonStr += "<img id='picMainImage' style='margin: 0 auto;' src='"+product.picMain+"' width='73.5' height='75'/>";
						jsonStr += "</li>";
						jsonStr += "<li>"+product.name+"</br>"+product.spec+"</li>";
						jsonStr += "<li><s>￥"+product.marketPrice+"</s></br>￥"+product.salePrice+"</li>";
						jsonStr += "<li>"+product.statusName+"</li>";
						jsonStr += "</ul>";
						jsonStr += "</a>";
					}
				}
				
                $('#detailBoxDiv').html(jsonStr);
                myScroll.refresh(totalCount);
            }
        }).fail(function() {
        	$("#detailBoxDiv").html("<font style='font-size:0.16rem;'>数据请求失败，请重新刷新</font>");
        })
    }
    // 下拉刷新事件
    myScroll.on("slideDown",function(){
        if(this.y > distance){
        	$("#pullUp-msg").html("上拉加载更多");
            downAjax();
            upIcon.removeClass("reverse_icon")
        }
    });
    // 上拉滑动事件
    myScroll.on("slideUp",function(){
        if(this.maxScrollY - this.y > distance){
            upAjax();
            upIcon.removeClass("reverse_icon")
        }
    });
}